package com.lyp.crm.conrtoller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lyp.crm.entity.Fenye;
import com.lyp.crm.entity.User;
import com.lyp.crm.service.Userdao;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
@Controller
@CrossOrigin(origins = "*")
public class UserController {
	@Autowired
	private Userdao userdao;
	@Autowired
	private User user;
	@Autowired
	private Fenye<User> fenye;
	SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	  Date date = new Date();
	  
	  
	  @ApiOperation(value="登录", notes="登录CRM后台系统")
		@ApiImplicitParams({
			@ApiImplicitParam(name = "uname", value = "用户姓名", required = true, dataType = "String", paramType = "path"), 
			@ApiImplicitParam(name = "upassword", value = "密码", required = true, dataType = "String", paramType = "path"), 
			@ApiImplicitParam(name = "ulock", value = "是否锁定", required = true, dataType = "Integer", paramType = "path"), 
			@ApiImplicitParam(name = "upassnum", value = "密码错误次数", required = true, dataType = "Integer", paramType = "path"), 
			
		})
	@ResponseBody
	@RequestMapping(value="/login",method=RequestMethod.POST)
	public Integer login(User user,String upassword,HttpSession session) {
		//��ȡ�û���
		String uname=user.getUname();
		//�����û������û�
		user = userdao.login(user);
		Integer i=0;
		//����������
		Integer upassnum=user.getUpassnum();
		//δ����������ȷ
		if (user.getUname().equals(uname)&&user.getUpassword().equals(upassword)&&user.getUlock()==1) {
			i=1;
			upassnum=0;
			//��ȡ��ǰ������������
			user.setUpassnum(upassnum);
			//�޸�
			userdao.updateupassnum(user);
			User user1=new User();
			user1.setUid(user.getUid());
			User login = userdao.login(user1);
			session.setAttribute("list", login);

			//������������ȷ
			
		} else if(user.getUname().equals(uname)&&user.getUpassword().equals(upassword)&&user.getUlock()==2) {
			i=2;
			upassnum++;
		
			user.setUpassnum(upassnum);
			userdao.updateupassnum(user);
			
			//�������
		}else {
			i=3;
			upassnum++;
			
			String string = sdf.format(date);
			user.setUlockdate(string);
			user.setUpassnum(upassnum);
			userdao.updateupassnum(user);
		}
		return i;
	}
	  @ApiOperation(value="员工查询", notes="根据员工姓名查询员工信息")
	  @ApiImplicitParam(name = "uname", value = "用户姓名", required = true, dataType = "String", paramType = "path")
	  @RequestMapping(value="/selectbyuname",method=RequestMethod.POST)
	  @ResponseBody
	  public User selectbyuname(String uname) {
		  user.setUname(uname);
		  return userdao.login(user);
	  }
	  @ApiOperation(value="员工查询", notes="根据员工姓名查询员工信息")
	  @ApiImplicitParam(name = "uid", value = "用户id", required = true, dataType = "Integer", paramType = "path")
	  @RequestMapping(value="/selectbyuid",method=RequestMethod.POST)
	  @ResponseBody
	  public User selectbyuid(Integer uid) {
		  return userdao.selectbyuid(uid);
	  }
	  
	  @ApiOperation(value="多条件查询员工", notes="根据数据多条件查询员工")
		@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "分页参数", required = true, dataType = "Integer", paramType = "path"), 
			@ApiImplicitParam(name = "rows", value = "分页参数", required = true, dataType = "Integer", paramType = "path"), 
			@ApiImplicitParam(name = "uname", value = "用户姓名", required = true, dataType = "String", paramType = "path"), 
			@ApiImplicitParam(name = "umax", value = "最大时间", required = true, dataType = "String", paramType = "path"), 
			@ApiImplicitParam(name = "umin", value = "最小时间", required = true, dataType = "Integer", paramType = "path"), 
			@ApiImplicitParam(name = "ulock", value = "是否锁定", required = true, dataType = "Integer", paramType = "path"), 
			@ApiImplicitParam(name = "usign", value = "签到", required = true, dataType = "Integer", paramType = "path"), 
			@ApiImplicitParam(name = "kaishishijian", value = "签到开始时间", required = true, dataType = "Integer", paramType = "path"), 
			@ApiImplicitParam(name = "jieshushijian", value = "签到结束时间", required = true, dataType = "Integer", paramType = "path"), 
			
		})
	@RequestMapping(value="/selectUser",method=RequestMethod.POST)
	@ResponseBody
	public  Fenye<User> selectUser(Integer page,Integer rows,User user){
		fenye.setPage((page-1)*rows);
		fenye.setPageSize(rows);
		fenye.setT(user);
		fenye=userdao.selectUser(fenye);
		return fenye;
		
	}
	
	  @ApiOperation(value="增加员工", notes="根据数据来增加员工")
		@ApiImplicitParams({
			@ApiImplicitParam(name = "uname", value = "用户姓名", required = true, dataType = "String", paramType = "path"), 
			@ApiImplicitParam(name = "usex", value = "性别", required = true, dataType = "Integer", paramType = "path"), 
			@ApiImplicitParam(name = "uage", value = "年龄", required = true, dataType = "Integer", paramType = "path"), 
			@ApiImplicitParam(name = "Usigndate", value = "签到时间", required = true, dataType = "String", paramType = "path"), 
			@ApiImplicitParam(name = "usign", value = "签到", required = true, dataType = "Integer", paramType = "path"), 
			@ApiImplicitParam(name = "upassword", value = "密码", required = true, dataType = "String", paramType = "path"), 
			@ApiImplicitParam(name = "ulock", value = "是否锁定", required = true, dataType = "Integer", paramType = "path"), 
			@ApiImplicitParam(name = "ulockdate", value = "被锁定时间", required = true, dataType = "String", paramType = "path"), 
			@ApiImplicitParam(name = "uenddate", value = "最后登录时间", required = true, dataType = "String", paramType = "path"), 
			@ApiImplicitParam(name = "usetdate", value = "账户创建时间", required = true, dataType = "String", paramType = "path"), 
			@ApiImplicitParam(name = "upassnum", value = "密码错误次数", required = true, dataType = "Integer", paramType = "path"), 
			@ApiImplicitParam(name = "umail", value = "邮箱", required = true, dataType = "String", paramType = "path"), 
			@ApiImplicitParam(name = "uphone", value = "手机号", required = true, dataType = "String", paramType = "path"), 
			@ApiImplicitParam(name = "ureserved1", value = "权重", required = true, dataType = "Integer", paramType = "path"), 
			@ApiImplicitParam(name = "utest", value = "备注", required = true, dataType = "String", paramType = "path"), 
			
		})
	@RequestMapping(value="/indeteUser",method=RequestMethod.POST)
	@ResponseBody
	public Integer indeteUser(User user) {
		//��ѯ�����û���Ϣ
		List<User> list = userdao.selectlist();
		//���ó�ʼ����ֵ
		Integer indeteUser=0;
		//���������û����ʹ�����û���ƥ��
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getUname().equals(user.getUname())) {
				 //������Ӱ������
				indeteUser=2;
			} 
		}
		
		if (indeteUser==0) {
			return indeteUser= userdao.indeteUser(user);
		}else{
			return indeteUser;
		}
		
	
	}
	 @ApiOperation(value="删除员工", notes="根据员工id来删除员工信息")
	 @ApiImplicitParam(name = "uid", value = "id", required = true, dataType = "Integer", paramType = "path") 
	@RequestMapping(value="/deleteUser",method=RequestMethod.POST)
	@ResponseBody
	public Integer deleteUser(Integer uid){
		
		return userdao.deleteUser(uid);
		
	}
	@ApiOperation(value="修改员工", notes="根据数据来修改员工信息")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "uid", value = "id", required = true, dataType = "Integer", paramType = "path"), 
		@ApiImplicitParam(name = "uname", value = "用户姓名", required = true, dataType = "String", paramType = "path"), 
		@ApiImplicitParam(name = "usex", value = "性别", required = true, dataType = "Integer", paramType = "path"), 
		@ApiImplicitParam(name = "uage", value = "年龄", required = true, dataType = "Integer", paramType = "path"), 
		@ApiImplicitParam(name = "Usigndate", value = "签到时间", required = true, dataType = "String", paramType = "path"), 
		@ApiImplicitParam(name = "usign", value = "签到", required = true, dataType = "Integer", paramType = "path"), 
		@ApiImplicitParam(name = "upassword", value = "密码", required = true, dataType = "String", paramType = "path"), 
		@ApiImplicitParam(name = "ulock", value = "是否锁定", required = true, dataType = "Integer", paramType = "path"), 
		@ApiImplicitParam(name = "ulockdate", value = "被锁定时间", required = true, dataType = "String", paramType = "path"), 
		@ApiImplicitParam(name = "uenddate", value = "最后登录时间", required = true, dataType = "String", paramType = "path"), 
		@ApiImplicitParam(name = "usetdate", value = "账户创建时间", required = true, dataType = "String", paramType = "path"), 
		@ApiImplicitParam(name = "upassnum", value = "密码错误次数", required = true, dataType = "Integer", paramType = "path"), 
		@ApiImplicitParam(name = "umail", value = "邮箱", required = true, dataType = "String", paramType = "path"), 
		@ApiImplicitParam(name = "uphone", value = "手机号", required = true, dataType = "String", paramType = "path"), 
		@ApiImplicitParam(name = "ureserved1", value = "权重", required = true, dataType = "Integer", paramType = "path"), 
		@ApiImplicitParam(name = "utest", value = "备注", required = true, dataType = "String", paramType = "path"), 
		
	})
	@RequestMapping(value="/updateUser",method=RequestMethod.POST)
	@ResponseBody
	public Integer updateUser(User user){
		//���ӵ�ǰʱ��
		user.setUsigndate(sdf.format(date));
		return userdao.updateUser(user);
		
	}
	
	@ApiIgnore
	@RequestMapping(value="/longin",method=RequestMethod.POST)
	@ResponseBody
	public Integer longin(HttpSession session){
		session.invalidate();
		return 1;
		
	}
	@ApiOperation(value="修改员工密码", notes="根据数据来修改员工的密码")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "uid", value = "id", required = true, dataType = "Integer", paramType = "path"), 
		@ApiImplicitParam(name = "upassword", value = "密码", required = true, dataType = "String", paramType = "path"), 
		@ApiImplicitParam(name = "upassnum", value = "密码错误次数", required = true, dataType = "Integer", paramType = "path"), 
		
	})
	@RequestMapping(value="/xiugai",method=RequestMethod.POST)
	@ResponseBody
	public Integer xiugai(User user){
		return userdao.xiugai(user);
		
	}
	
	@ApiOperation(value="重置员工密码", notes="根据员工id来重置员工密码")
	@ApiImplicitParam(name = "uid", value = "id", required = true, dataType = "Integer", paramType = "path") 
	@RequestMapping(value="/chongzhi",method=RequestMethod.POST)
	@ResponseBody
	public Integer chongzhi(User user){
		
		return userdao.chongzhi(user);
		
	}
	
	@ApiOperation(value="员工签到", notes="根据数据来修改员工的签到字段")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "uid", value = "id", required = true, dataType = "Integer", paramType = "path"), 
		@ApiImplicitParam(name = "Usigndate", value = "签到时间", required = true, dataType = "String", paramType = "path"), 
		
	})
	@RequestMapping(value="/qiandaoq1",method=RequestMethod.POST)
	@ResponseBody
	public Integer qiandaoq1(User user){
		String date=new Date().toLocaleString();
	
		
		user.setQiandaokaishishijian(date);
		
		return userdao.qiandao(user);
		
	}
	@ApiOperation(value="员工查询", notes="用来查询所有员工")
	@RequestMapping(value="/selectlist",method=RequestMethod.POST)
	@ResponseBody
	public List<User> selectlist() {
		// TODO Auto-generated method stub
		return userdao.selectlist();
	}
	@ApiOperation(value="员工签退", notes="用来让所有员工都签退")
	@RequestMapping(value="/quanbuqiantui",method=RequestMethod.POST)
	@ResponseBody
	public Integer quanbuqiantui() {
		// TODO Auto-generated method stub
		return userdao.quanbuqiantui();
	}
	
	@ApiOperation(value="查询咨询师", notes="用来查询所有的咨询师")
	@RequestMapping(value="/selectzixunshi",method=RequestMethod.POST)
	@ResponseBody
	public List<User> selectzixunshi() {
		// TODO Auto-generated method stub
		return userdao.selectzixunshi();
	}
}
